import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ApiService, urlService } from 'src/app/core/api';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss'],
})
export class LoginComponent implements OnInit {
    loginForm: FormGroup;
    validId;
    validateCodeImg;
    imageVerifyCodeId;
    ngOnInit() {
        this.loginForm = this.fb.group({
            loginId: [null,
              [Validators.required]
            ],
            loginPw: [null, [Validators.required]],
            imageVerifyCodeId:[null,[Validators.required]],
            imageCode:[null,[]]
        });
        this.service.post('生成图形验证码唯一id').then((res:any)=>{
          this.validId = res.data.imageVerifyCodeId;
          this.loginForm.patchValue({imageVerifyCodeId:this.validId});
          this.validateCodeImg = urlService('dev', '生成图形验证码')+'?imageVerifyCodeId='+this.validId;
        })
    }
    getvalidateCode(){
      this.validateCodeImg = urlService('dev', '生成图形验证码')+'?imageVerifyCodeId='+this.validId+'&time='+new Date().getTime();
    }
    isShowPSD=false;
    onShowPSW(value){
      this.isShowPSD = false;
      this.loginForm.setValue(value);
    }
    onHidePSW(value){
      this.isShowPSD = true;
      this.loginForm.setValue(value);
    }
    submit() {
      if (this.loginForm.valid) {
        this.service.post('登录', this.loginForm.value).then((res: any) => {
          localStorage.setItem('token', res.data.token);
          localStorage.setItem('user', JSON.stringify({auths: res.data.auths, loginId: res.data.loginId}));
          this.router.navigate(['home/index']);
        });
      }
    }
    constructor(private fb: FormBuilder, private router: Router, private service: ApiService, private message: NzMessageService) {}
}
